package pl.lodz.p.beans;

import java.io.Serializable;
import java.util.List;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import org.apache.log4j.Logger;

import pl.lodz.p.beans.utils.Util;
import pl.lodz.p.model.Patient;
import pl.lodz.p.model.User;
import pl.lodz.p.services.PatientService;
import pl.lodz.p.services.PatientServiceImpl;
import pl.lodz.p.services.UserService;
import pl.lodz.p.services.UserServiceImpl;

public class PatientManagedBean implements Serializable{

	private static final long serialVersionUID = -1843740389268153315L;

	private User loggedUser;
	
	private Patient patient;
	
	private PatientService patientService = new PatientServiceImpl();
	
	private UserService userService = new UserServiceImpl();
	
	public PatientManagedBean() {
		loggedUser = ((LoginBean) getBean("loginBean")).getLoginUser();
	}
	
	public static Object getBean(String expr){
	    FacesContext context = FacesContext.getCurrentInstance();
	    Application app = context.getApplication();
	    ValueBinding binding = app.createValueBinding("#{" + expr + "}");
	    Object value = binding.getValue(context);
	    return value;
	}
	
	public User getLoggedUser() {
		return loggedUser;
	}

	public void setLoggedUser(User loggedUser) {
		this.loggedUser = loggedUser;
	}

	private static Logger log = Logger.getLogger(PatientManagedBean.class);
	
	public List<Patient> getMyPatients(){
		LoginBean bean = (LoginBean) Util.getBean("loginBean");
		loggedUser = bean.getLoginUser();
		loggedUser = userService.update(loggedUser);
		return loggedUser.getPatients();
	}

	public Patient getPatient() {
		return patient;
	}

	public void setPatient(Patient patient) {
		this.patient = patient;
	}
}
